Can you post the code at line 190 in search_results_controller. Rb, and any others that possibly refers to the "fields" attribute in your show method? Also relevant parts of your model FeededProduct from app/models/feeded_product.rb.
Up vote 3 down vote favorite 1 share g+ share fb share tw.
Using: Rails 3.1.1 I am trying to create a search engine in my application that browses a large database (apprx 100 000 items) for string matches. I am using the following: fp = FeededProduct. Where("name LIKE '%blue%' OR description LIKE '%blue%' OR category LIKE '%blue%'") for the search query for "blue".
When I run the equivalent search phrase in MySQL it works fine but when I try to run this in rails it shows: NoMethodError: undefined method `fields' for nil:NilClass: SELECT `feeded_products`. * FROM `feeded_products` WHERE (name LIKE '%blue%' OR description LIKE '%blue%' OR category LIKE '%blue%') Clues and troubleshooting: This happens only for large search results, I have not been able to distinguish a number but it crashes when it should have returned 920 results but it does NOT crash when returning 6 results! My conclusion of the above is either that it cannot keep all the 920 results in the memory OR that there is some type of row that makes it crash and the more results, the more likely it is that it will contain that type of row.
I am leaning more towards the first conclusion. I cannot troubleshoot it very well because it crashes (with the same error code) when I try to run: raise fp. Inspect It also crashes for: fp.
Each do |prod| begin puts 'Do nothing' rescue puts 'Something crashed' end but it does NOT crash for: raise fp.count. Inspect So, am I having a memory type of problem? What can I do to trouble shoot this further and/or solve the problem?
Using: Mac OS X 10.7.2. Lion Database: InnoDB Adapter: Mysql2 (don't know which version) Stack: ActiveRecord::StatementInvalid (NoMethodError: undefined method fields' for nil:NilClass: SELECT feeded_products`. * FROM feeded_products WHERE (name LIKE '%blue%' OR description LIKE '%blue%' OR category LIKE '%blue%')): app/controllers/search_results_controller.
Rb:190:in `show' Edit 2012-03-06 Additional trouble shooting: I tried with fp2 = FeededProduct. Limit(60000) to create a really big array of hits and it worked fine. So I guess that rules out my guess that the fp variable cannot hold a certain amount of items.
The core of the problem seems to be that if I use the: fp = FeededProduct. Where("name LIKE '%blue%' OR description LIKE '%blue%' OR category LIKE '%blue%'") I cannot use the fp-variable for anything afterwards without the application crashing. Ruby-on-rails memory nomethoderror link|improve this question edited Mar 6 at 12:19 asked Feb 29 at 15:10Christoffer879 97% accept rate.
– Larry K Feb 29 at 15:17 Yes, I am in development mode. Actually, I don't really know what the stack trace is (googling it right now). If you refer to the webserver terminal output it says: ActiveRecord::StatementInvalid (NoMethodError: undefined method fields' for nil:NilClass: SELECT feeded_products`.
* FROM feeded_products WHERE (name LIKE '%blue%' OR description LIKE '%blue%' OR category LIKE '%blue%')): app/controllers/search_results_controller. Rb:190:in `show' – Christoffer Feb 29 at 15:24 ...it should perhaps be mentioned that this is my 2nd large Rails project and that I am still learning rails. – Christoffer Feb 29 at 15:25 which version of mysql gem are you using?
Which operating system are you using? And which storage engine are you using for large database search? Like MyISAM or InnoDB?
Think for mysql2 (note the "2") gem - (github.com/brianmario/mysql2) – Amit Feb 29 at 17:30 The stacktrace in the log should be many many lines long. The line you gave "StatementInvalid...." is just the first line of the trace. – Larry K Feb 297 at 4:00.
Can you post the code at line 190 in search_results_controller. Rb, and any others that possibly refers to the "fields" attribute in your show method? Also relevant parts of your model FeededProduct from app/models/feeded_product.
Rb app/controllers/search_results_controller. Rb:190:in `show' It's not clear what fields refers to from the info you posted. It could be a typo, bad code or a migration that needs to be run.
Also note that fp.each. Do |prod| is not syntactically correct. It should be: fp.
Each do |prod.
Edk750: Row #190 is just the respond_to do |format| format. Html # show.html. Erb format.
Json { render :json => @search_result } end – Christoffer Mar 6 at 12:01 Regarding the fp.each. Do it was just a typo (I wrote it manually here on SO, it's ok in the code). – Christoffer Mar 6 at 12:01 The big thing is that I do not use any method, attribute or anything called "fields".
I have not named anything "fields" and there is nothing in the db that is called fields. Basically, the string "fields" cannot be found anywhere in the code throughout the whole application. – Christoffer Mar 6 at 12:03 I think the standing point is that if I make a search that would make the "fp"-variable get some 5-10 results, it works fine.
If I make a search that returns more hits, it will crash with the error message given. It seems to me that there is something wrong with the adapter, mysql or similar that cannot handle too many results. Is there a limit here?
Does the fact that I am using "LIKE" affect anything? – Christoffer Mar 6 at 12:05 There's no inherent limitation in the stack that I've come across that resembles anything you're describing. I strongly suspect it's something specific to your code or some gem you're using.
Given that a FeededProduct is derived from ActiveRecord::Base, and you're sending a . Where to the class, you should receive a ActiveRecord::Relation object. This allows you to iterate over the results.
The key here is "NoMethodError: undefined method fields' for nil:NilClass" who is calling fields on a nil? This must be in your code somewhere, either directly or indirectly. We need more info.
– edk750 5-108 at 7:06.
Class Item Feeded_products(query) Hope this helps.
I will try that out as well, thanks! – Christoffer Mar 14 at 14:24 Tried it now but that didn't work either :( Same error... – Christoffer Mar 15 at 10:18.
For these situations in which you have large sets of objects you can consider the use of find_each and find_in_batches methods or some kind of pagination.
1 Thanks, I will try that as well! – Christoffer Mar 14 at 14:24 I guess that would have solved the problem, if it would have been a memory problem. I guess it isn't after all...the solution didn't work unfortunately.
Thanks anyway! – Christoffer Mar 15 at 5:51.
I changed to mysql instead of mysql2 adapter and it solved the problem. Thanks everyone for trying! I learned a lot trouble shooting your suggestions.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.